package easy.com.sk.controller;

import com.alibaba.fastjson.JSON;
import com.easy.common.service.SKService;
import com.easy.common.util.Result;
import com.easy.common.util.SKRequest;
import jakarta.servlet.http.HttpServletRequest;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.UUID;
// 秒杀控制类
@RestController
@RequestMapping("sk")
public class SKController {
    @Autowired
    SKService skService;
    /**
     *   /sk/12    12就是秒杀商品id  skconcertid
     * 秒杀
     * @param concertid  秒杀数据   秒杀商品的id
     * @param request    请求对象  获取token 获取userid
     * @return
     */
    @PostMapping("/concertid")
    public Result sk(@PathVariable("concertid") Integer concertid, HttpServletRequest  request)
    {
        //封装SKResqust 对象
        // 获取登录用户的id值
        Integer userid =1;
//        getUserid(request);
        //生成秒杀记录id
        String skid = UUID.randomUUID().toString();//可以自定义算法生成秒杀记录id。
        SKRequest skRequest = new SKRequest(userid,concertid,skid);
        //利用了阿里巴巴的fastJSON将SKRequest转换成JSON字符串，将消息发送到MQ  交给service发送
        String message= JSON.toJSONString(skRequest);
        skService.sendMSG(message);
        //响应请求
        return Result.success(skid);
    }
}














































